var salt = sessionStorage.getItem('usersalt');
console.log(salt);
///perilInfo/getInfo
var arr3 = new Array();
arr3 = [0, 1, 2];
arr1 = [];

layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		formSelects = layui.formSelects,
		laytpl = layui.laytpl,
		table = layui.table;

	/*验证登录*/
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);

	}

	/*时间选择器*/
	laydate.render({
		elem: '#statdate',
		trigger: 'click'

	});

	laydate.render({
		elem: '#enddate',
		trigger: 'click'

	});

	/*筛选*/
	$('#selerisk').on('click', function() {

		yinhuanlist();

	})

	/*搜索*/
	$('#findscri').on('click', function() {
		var inptext = $('#inptext').val();
		var box = /\s/g;
		var res = inptext.replace(box, '');
		yinhuanlist(res);

	});
	var company;
	/*取出所有的单位*/
	finddanwei();

	function finddanwei() {
		$.ajax({
			url: url + 'baseEnt/getAllEnt',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {

			},
			success: function(data) {
				$('#danwe').empty();
				console.log(data);
				var opt = '<option value="0" selected="selected" >请选择</option>';
				if(data.code == 0) {
					var list = data.data;
					company = list;
					//					console.log(list);
					//					for(var i = 0; i < list.length; i++) {
					//						opt += '<option value="' + list[i].id + '">' + list[i].name + '</option>';
					//					}
					//					$('#danwe').append(opt);
					//					form.render();

				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}

	var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onCheck: zTreeOnCheck
		}
	};
	console.log(company);
	var zNodes = company;

	$.fn.zTree.init($("#danwe"), setting, zNodes);

	function getval() {
		arr = []
		var treeObj = $.fn.zTree.getZTreeObj("danwe");
		var nodes = treeObj.getCheckedNodes(true),
			v = "";
		for(var i = 0; i < nodes.length; i++) {
			v += nodes[i].name + ",";
			console.log()
			arr.push(nodes[i].id); //获取选中节点的值
		}
		return arr;
	}

	$.fn.zTree.init($("#danwe"), setting, zNodes);
	$('#resclas').click(function() {

		$('#danwe').show();
		//		$('#resclas').val('')
	});
	$('#quedingqy').click(function() {
		$('#danwe').hide();
	});

	var n = ''

	function zTreeOnCheck(event, treeId, treeNode) {
		//		nams = "";
		//		var checked = treeNode.checked;
		//			console.log((treeNode?treeNode.name:"root"));
		//				nams+= (treeNode?treeNode.name:"root") + ",";
		var treeObj = $.fn.zTree.getZTreeObj("danwe");
		var nodes = treeObj.getCheckedNodes(true),
			n = "";
		for(var i = 0; i < nodes.length; i++) {
			n += nodes[i].name + ",";
			$('#resclas').val(n)

		}

	};

	var btnyes = 0;
	var btnno = 0;
	var btnclear = 0;
	/*隐患类型按钮*/
	$('#btnyes').on('click', function() {
		btnyes++;
		console.log(btnyes);
		if(btnyes % 2 == 0) {
			$('#triangle').css('display', 'block');
			$('#btnyes').css('background', 'white');
			arr3.push(0);
			console.log(arr3);
		} else {

			$('#triangle').css('display', 'none');
			$('#btnyes').css('background', 'gray');

			for(var i = 0; i < arr3.length; i++) {
				if(arr3[i] == 0) {
					arr3.splice(i, 1);
					console.log(arr3);
				}
			}
		}

	});
	$('#btnno').on('click', function() {
		btnno++;
		console.log(btnno);
		if(btnno % 2 == 0) {
			$('#triangle1').css('display', 'block');
			$('#btnno').css('background', 'white');
			arr3.push(1);
			console.log(arr3);
		} else {
			$('#triangle1').css('display', 'none');
			$('#btnno').css('background', 'gray');
			for(var i = 0; i < arr3.length; i++) {
				if(arr3[i] == 1) {
					arr3.splice(i, 1);
					console.log(arr3);
				}
			}
		}

	});
	$('#btnclear').on('click', function() {
		btnclear++;
		console.log(btnclear);
		if(btnclear % 2 == 0) {
			$('#triangle2').css('display', 'block');
			$('#btnclear').css('background', 'white');
			arr3.push(2);
			console.log(arr3);
		} else {
			$('#triangle2').css('display', 'none');
			$('#btnclear').css('background', 'gray');
			for(var i = 0; i < arr3.length; i++) {
				if(arr3[i] == 2) {
					arr3.splice(i, 1);
					console.log(arr3);
				}
			}
		}

	});

	var tableIns;
	yinhuanlist();

	function yinhuanlist(soutext) {
		var ls = getval();
		tableIns = table.render({
			elem: '#newsList',
			method: 'post',
			url: url + 'operateCard/getPage',
			where: {
				cardType: 1,
				title: $('#inptext').val(),
				type: $('#type').val(), //类型
				entId: ls.toString(), //单位id
				post: getvalPost().toString(),
				Token: salt, //用户码

			},
			cellMinWidth: 95,

			page: true,

			height: "full-115",

			id: "newsListTable",
			cols: [
				[{
						type: "numbers",
						width: 50,
						title: "编号"
					},
					{
						type: "checkbox",
						width: 50
					}, {
						field: 'title',
						title: '应急处置内容',
						align: 'center',
						templet: function(p) {
							return "<div onclick=lookxiangqing('" + p.id + "')>" + p.title + "</div>";
						}

					}, {
						field: 'controlled',
						title: '受控号',
						align: 'center',
						templet: function(p) {
							return "<div onclick=lookxiangqing('" + p.id + "')>" + p.controlled + "</div>";
						}
					}, {
						field: 'type',
						title: '卡类别',
						align: 'center',
						templet: function(p) {
							if(p.type == '0') {
								return "<div onclick=lookxiangqing('" + p.id + "')>一般卡</div>";
							}
							if(p.type == '1') {
								return "<div onclick=lookxiangqing('" + p.id + "')>关键卡</div>";
							}
						}
					}, {
						field: 'releaseTime',
						title: '发布时间',
						align: 'center',
						templet: function(p) {
							return "<div onclick=lookxiangqing('" + p.id + "')>" + p.releaseTime + "</div>";
						}
					}, {
						field: 'reviseTime',
						title: '修订时间',
						align: 'center',
						templet: function(p) {
							return "<div onclick=lookxiangqing('" + p.id + "')>" + p.reviseTime + "</div>";
						}
					}, {
						field: 'postName',
						title: '涉及单位',
						align: 'center',
						templet: function(p) {
							return "<div onclick=lookxiangqing('" + p.id + "')>" + p.postName + "</div>";
						}
					}, {
						title: '操作',
						align: 'center',
						toolbar: '#toolbar'
					}
				]
			],
			done: function(res, curr, count) {
				//无论是异步请求数据，还是直接赋值数据，都会触发该回调。可以利用该回调做一些表格以外元素的渲染。
				//如果是异步请求数据方式，res即为你接口返回的信息。
				//如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
				console.log(res.data);

				//
				//                  //得到当前页码
				console.log(curr);
				//
				//                  //得到数据总量
				//                  console.log(count);
			}
		});

	}

	//是否置顶
	form.on('switch(newsTop)', function(data) {
		var index = layer.msg('修改中，请稍候', {
			icon: 16,
			time: false,
			shade: 0.8
		});
		setTimeout(function() {
			layer.close(index);
			if(data.elem.checked) {
				layer.msg("置顶成功！");
			} else {
				layer.msg("取消置顶成功！");
			}
		}, 500);
	})

	//编辑
	$(".editAll_btn").click(function() {
		var checkStatus = table.checkStatus('newsListTable'),
			data = checkStatus.data;
		console.log(data);
		if(data.length == 0) {
			layer.msg("请选择需要编辑的信息！");
			return;
		}
		if(data.length > 1) {
			layer.msg("每次只能编辑一条信息！");
			return;
		}
		window.location.href = '../add/add.html?id=' + data[0].id;
	})
	//批量删除
	$(".delAll_btn").click(function() {
		var checkStatus = table.checkStatus('newsListTable'),
			data = checkStatus.data,

			newsId = [];
		console.log(data);
		var arr2 = new Array();
		for(var i = 0; i < data.length; i++) {
			arr2.push(data[i].id);
		}
		if(data.length > 0) {
			for(var i in data) {
				newsId.push(data[i].newsId);
			}
			layer.confirm('确定删除选中的数据吗？', {
				icon: 3,
				title: '提示信息'
			}, function(index) {
				//                 $.get("删除文章接口",{
				//                     newsId : newsId  //将需要删除的newsId作为参数传入
				//                 },function(data){

				$.ajax({
					url: url + 'operateCard/del',
					dataType: 'json',
					async: false,
					type: "POST",
					data: {
						id: arr2.toString(),
						Token: salt
					},
					success: function(data) {
						console.log(data);
						if(data.code == 0) {
							layer.msg('删除成功');
						} else {
							layer.msg(data.msg);
						}

					},
					error: function(error) {
						console.log("获取失败");
					}
				});

				tableIns.reload();
				layer.close(index);
				//                 })
			})
		} else {
			layer.msg("请选择需要删除的数据");
		}
	})

	//列表操作
	table.on('tool(newsList)', function(obj) {
		var layEvent = obj.event,
			data = obj.data;

		if(layEvent === 'edit') { //编辑

			$.get(url + '/attachment/seekFile2/' + data.attId, function(data) {
				window.open(url + "file/" + data);
			});

		} else if(layEvent === 'del') { //下载
			window.open(url + '/attachment/seekFile/' + data.attId);
		} else if(layEvent === 'look') { //预览
			layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
		}
	});

})

function lookxiangqing(id) {
	window.location.href = '../show/show.html?id=' + id;
}

function showdescribe(self, cont) {
	console.log(cont + "--------------");

	layui.use(['layer', 'form'], function() {
		var layer = layui.layer;
		layer.tips(cont, "#" + self, {
			tips: [1, '#ccc'],
			time: 10000,
			area: ['auto', 'auto'] //这个属性可以设置宽高  auto 表示自动
		});
	});
}

function hidedescribe(self, cont) {
	//	$('#' + self).css('display', 'none');
	layer.closeAll();
}

var postSetting = {
	check: {
		enable: true
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	callback: {
		onCheck: zTreeOnCheckPost
	}
};

function zTreeOnCheckPost(event, treeId, treeNode) {
	//	var checked = treeNode.checked;
	//	console.log((treeNode ? treeNode.name : "root"));
	//	danweim = treeNode ? treeNode.name : "root"
	//	$('#resclas').val(danweim)
	//	$('#resclass').hide();

	var treeObj = $.fn.zTree.getZTreeObj("resclass");
	var nodes = treeObj.getCheckedNodes(true),
		n = "";
	for(var i = 0; i < nodes.length; i++) {
		n += nodes[i].name + ",";
		$('#post').val(n)

	}

};
getallentPost();
var companyPost;
$.fn.zTree.init($("#resclass"), postSetting, companyPost);
$('#post').click(function() {
	$('#resclass').show();
	//$('#resclas').val('')
	//$.fn.zTree.init($("#resclass"), setting, zNodes);
});

function getallentPost() {

	$.ajax({
		url: url + 'post/getAllEnt',
		dataType: 'json',
		async: false,
		type: "POST",
		data: {

		},
		success: function(data) {
			$('#resclass').empty();
			if(data.code == 0) {
				var list = data.data;
				companyPost = list;

			}

		},
		error: function(error) {
			console.log("获取失败");
		}
	});

}

function getvalPost() {
	arr = []
	var treeObj = $.fn.zTree.getZTreeObj("resclass");
	var nodes = treeObj.getCheckedNodes(true),
		v = "";
	for(var i = 0; i < nodes.length; i++) {
		v += nodes[i].name + ",";
		console.log()
		arr.push(nodes[i].id); //获取选中节点的值
	}
	return arr;
}